/*
 * Copyright (C) 2003, Velocityme Partnership
 * ActionSessionBean.java
 *
 * Created on 30 April 2003, 07:43
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="ActionSession"
 *           jndi-name="ejb/ActionSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class ActionSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private SearchSessionLocal m_searchSessionLocal;
    
    private PermissionLocal m_permissionActionCreate;
    private PermissionLocal m_permissionActionEdit;
    private PermissionLocal m_permissionNodeView;

    /**
     * If the user has permission to create a new Action in the specified parent
     * node then go ahead and create it and log an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Action
     * @param p_actionValue The value details for the Action part of the action
     *
     * @ejb.interface-method view-type="local"
     **/
    public ActionLocal createAction(KeySessionLocal p_keyLocal,
                               NodeLocal p_parentNodeLocal,
                               com.velocityme.valueobjects.ActionValue p_actionValue) 
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_parentNodeLocal).contains(m_permissionActionCreate)) {
                DBObjectLocal dBObjectLocal = DBObjectUtil.getLocalHome().create();
                TaskValue taskValue = p_actionValue.getTaskValue();
                taskValue.setType("Action");
                taskValue.getNodeValue().setFinalType("Action");
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE, p_keyLocal.getUserLocal(), dBObjectLocal);
                TaskLocal taskLocal = TaskSessionUtil.getLocalHome().create().createTaskHelper(p_keyLocal, p_parentNodeLocal, taskValue, changeDeltaLocal);
                ActionLocal actionLocal = ActionUtil.getLocalHome().create(taskLocal, p_actionValue);
                return actionLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to edit the action then do so and log the
     * changes with an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_action The Action object being editted
     * @param p_actionValue The value details for the Action part of the action
     *
     * @ejb.interface-method view-type="local"
     **/
    public void editAction(KeySessionLocal p_keyLocal, 
                           ActionLocal p_actionLocal,
                           com.velocityme.valueobjects.ActionValue p_actionValue,
                           java.util.Collection p_responsibleContactableLocals,
                           java.util.Collection p_interestedContactableLocals)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            TaskLocal taskLocal = p_actionLocal.getTaskLocal();
            NodeLocal nodeLocal = taskLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionActionEdit)) {
                TaskValue taskValue = p_actionValue.getTaskValue();
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.EDIT, p_keyLocal.getUserLocal(), nodeLocal.getDBObjectLocal());
                TaskSessionUtil.getLocalHome().create().editTaskHelper(p_keyLocal, taskLocal, taskValue, p_responsibleContactableLocals, p_interestedContactableLocals, changeDeltaLocal);
                p_actionLocal.setActionValue(p_actionValue);
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to access an action then return the local object.
     *
     * @param p_key User's security key
     * @param p_actionPK The Action object to lookup
     *
     * @ejb.interface-method view-type="local"
     **/
    public ActionLocal getActionLocal(KeySessionLocal p_keyLocal, 
                                      ActionPK p_actionPK)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            ActionLocal actionLocal = ActionUtil.getLocalHome().findByPrimaryKey(p_actionPK);
            NodeLocal nodeLocal = actionLocal.getTaskLocal().getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionNodeView)) {
                return actionLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionActionCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ACTION_CREATE)));
            m_permissionActionEdit = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ACTION_EDIT)));
            m_permissionNodeView = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.NODE_VIEW)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
